package com.tea.room.util;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class ConfigUtil {

    private static String DEFAULT_PATH = "config/";
    private static Properties properties = null;
    public ConfigUtil() {
    }

    private static synchronized void initialize(String prop) {
        InputStream is = Thread.currentThread().getContextClassLoader()
                .getResourceAsStream(prop);
        if (is == null) {
            return;
        }
        properties = new Properties();
        try {
            properties.load(is);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }finally{
            try{
                if(is!=null)
                    is.close();
            }catch(Exception ex){
                throw new RuntimeException(ex);
            }
        }
    }

    public static synchronized Properties getProperties(String prop)
            throws RuntimeException {
        initialize(DEFAULT_PATH + prop);
        return properties;
    }
    
    /**
     * 
     * @param fileName  文件名称
     * @param key 键名
     * @return 键值
     */
    public static synchronized String getPropertyValue(String fileName,String key){
        Properties prop = getProperties(fileName);
        return prop.getProperty(key);
    }
    
    /**
     * 测试
     */
    public static void main(String[] args) {
        
        Properties prop = ConfigUtil.getProperties("wechat.properties");
        System.err.println(prop.get("ACCESS_TOKEN_URL"));
    }
    
    
    
//    public static void main(String[] args) {
//        HttpClient client = HttpClient.getInstance();
//        String params = "?grant_type=client_credential&appid="+ APPID +"&secret=" + APPSECRET;
//        
//        String json = client.sendHttpGet(ACCESS_TOKEN_URL + params);
//        System.out.println(client.sendHttpGet(ACCESS_TOKEN_URL + params));;
//    }
    
//    public static void main(String[] args) {
//        HttpClient client = HttpClient.getInstance();
//        
//        HashMap<String,String> maps = new HashMap<String,String>();
//        maps.put("appid", APPID);
//        maps.put("appsecret", APPSECRET);
//        String result = client.sendHttpPost(PMS_ACCESS_TOKEN_URL, maps);
//        System.out.println(result);
//    }
    
    
    
}
